package com.example.tao_city.text1;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import static com.example.tao_city.text1.R.id.fm1_chaxun_bangka;

/**
 * Created by TAO_CITY on 2018/1/15.
 */

public class Fm1_Chaxun extends FragmentActivity implements View.OnClickListener{
    private ImageView back,lin1,lin2,lin3,heimu;
    private TextView bangka,tieka,dukaqi;
    private Fragment chaxun1,chaxun2,chaxun3;

    public static boolean ma=false;
    public static boolean runing=true;
    public static boolean an=false;



    private Handler handler;
    private boolean tuichu=true;
    private int i=0;




    @Override
    protected void onCreate(Bundle save){
        super.onCreate(save);
        setContentView(R.layout.fm1_chaxun);
        initbutton();
        geti(1);
        handler=new Handler();
        handler.postDelayed(run,1);

    }
    private void initbutton(){
        back=findViewById(R.id.fm1_chaxun_back);
        heimu=findViewById(R.id.fm1_chaxun_heimu);

        bangka=findViewById(fm1_chaxun_bangka);
        tieka=findViewById(R.id.fm1_chaxun_tieka);
        dukaqi=findViewById(R.id.fm1_chaxun_dukaqi);
        lin1=findViewById(R.id.chaxun_bangkaline);
        lin2=findViewById(R.id.chaxun_tiekaline);
        lin3=findViewById(R.id.chaxun_dukaqiline);


        /*chaxun1=new Chaxun_bangka();
        chaxun2=new Chaxun_tieka();
        chaxun3=new Chaxun_dukaqi();*/


        bangka.setOnClickListener(this);
        tieka.setOnClickListener(this);
        dukaqi.setOnClickListener(this);
        back.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.fm1_chaxun_back:
                finish();
                break;
            case fm1_chaxun_bangka:
                geti(1);
                break;
            case R.id.fm1_chaxun_tieka:
                geti(2);
                break;
            case R.id.fm1_chaxun_dukaqi:
                geti(3);
                break;
        }

    }
    private void geti(int i){
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction transaction=fm.beginTransaction();
        hidefm(transaction);//隐藏所有视图
        switch (i){
            case 1:
                setcolor();//隐藏全部横线和文字变暗
               if(chaxun1==null){//判空处理
                    chaxun1=new Chaxun_bangka();//初始化视图
                    transaction.add(R.id.fm1_chaxun_fml,chaxun1);//添加到指定布局位置
                }else {
                    transaction.show(chaxun1);//显示当前视图
                }
               //transaction.show(chaxun1);
                runing=false;
                bangka.setTextColor(Color.parseColor("#458fee"));//文字颜色改变
                lin1.setVisibility(View.VISIBLE);//显示该部分横线
                break;
            case 2:
                setcolor();
                if(chaxun2==null){
                    chaxun2=new Chaxun_tieka();
                    transaction.add(R.id.fm1_chaxun_fml,chaxun2);
                }else {
                    transaction.show(chaxun2);
                }
                runing=true;
                //transaction.show(chaxun2);
                tieka.setTextColor(Color.parseColor("#458fee"));
                lin2.setVisibility(View.VISIBLE);
                break;
            case 3:
                setcolor();
                if(chaxun3==null){
                    chaxun3=new Chaxun_dukaqi();
                    transaction.add(R.id.fm1_chaxun_fml,chaxun3);
                }else {
                    transaction.show(chaxun3);
                }
                runing=false;
                //transaction.show(chaxun3);
                dukaqi.setTextColor(Color.parseColor("#458fee"));
                lin3.setVisibility(View.VISIBLE);
                break;
        }
        transaction.commit();//关闭事务
    }

    private void hidefm(FragmentTransaction transaction) {

        /*if(ma){
            transaction.add(R.id.fm1_chaxun_fml,chaxun1);
            transaction.add(R.id.fm1_chaxun_fml,chaxun2);
            transaction.add(R.id.fm1_chaxun_fml,chaxun3);
            ma=false;
        }*/
        if(chaxun1!=null){
            transaction.hide(chaxun1);
        }
        if(chaxun2!=null){
            transaction.hide(chaxun2);
        }
        if(chaxun3!=null){
            transaction.hide(chaxun3);
        }

    }

    private void setcolor(){
        bangka.setTextColor(Color.parseColor("#808080"));
        tieka.setTextColor(Color.parseColor("#808080"));
        dukaqi.setTextColor(Color.parseColor("#808080"));
        lin1.setVisibility(View.INVISIBLE);
        lin2.setVisibility(View.INVISIBLE);
        lin3.setVisibility(View.INVISIBLE);

    }
    Runnable run=new Runnable() {
        @Override
        public void run() {
            if(tuichu) {
                if (an) {
                    if(i==0) {

                        //heimu.setVisibility(View.VISIBLE);
                        back.setEnabled(false);
                        bangka.setEnabled(false);
                        tieka.setEnabled(false);
                        dukaqi.setEnabled(false);
                        i=1;
                        an = false;
                    }else {
                        heimu.setVisibility(View.INVISIBLE);
                        back.setEnabled(true);
                        bangka.setEnabled(true);
                        tieka.setEnabled(true);
                        dukaqi.setEnabled(true);
                        i=0;
                        an = false;
                    }
                }
                handler.postDelayed(this, 500);
            }
        }
    };



    @Override
    protected void onPause() {
        super.onPause();
        tuichu=false;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        tuichu=true;
        ma=false;
        runing=true;
        an=false;
        handler=new Handler();
        handler.postDelayed(run,1);

    }


  }
